package com.ubao.activity;

import java.io.File;

import com.ubao.R;
import com.ubao.activity.MyApp.GoBack;
import com.ubao.activity.main.HorzScrollWithListMenu;
import com.ubao.activity.map.MyMapActivity;
import com.ubao.base.RequestBaseActivity;
import com.ubao.bean.Commodity;
import com.ubao.global.GoBackable;
import com.ubao.network.JsonParser;
import com.ubao.network.ResponseInterface;
import com.ubao.network.Url;
import com.ubao.network.req.SpecialRequest;
import com.ubao.network.resp.SpecialResponse;
import com.ubao.ui.ratingbar.Ratingbar;
import com.ubao.ui.titlebar.GTitleBar;
import com.ubao.util.log.Logger;
import com.youxilua.common.fetch.CacheFetch;
import com.youxilua.waterfall.Constants;
import com.youxilua.waterfall.item.FlowView;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

public class ProductDetailActivity extends RequestBaseActivity implements OnClickListener,GoBackable {
	Ratingbar mRatingBar;
    GTitleBar mTitleBar;
    Commodity commodity;
    TextView tvCheckTimes;
    Bitmap bitmap;
    Drawable drawable;
    ImageView ivProductImg;
	  @Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ui_productdetail);
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();
		commodity = (Commodity) bundle.getSerializable(Commodity.COMMODITY);
		Logger.d("id:" + commodity.getCommodityID());
		initViews();
		//sendDetailRequest();
		LoadImage();
	}
	  
	  public void LoadImage() {
			new LoadImageThread().start();
	}

	private void sendDetailRequest() {
		startProgress();
		SpecialRequest request = new SpecialRequest(Url.url_commoditydetail_full);
		request.postParams = Url.getCommoditydetail(this, commodity.getCommodityID(), MyApp.locationManager.longtitude, MyApp.locationManager.latitude);
		mHttpHandler.sendRequest(request);
	}

	private void initViews() {
		mRatingBar = (Ratingbar) findViewById(R.id.mRatingbar);
		mRatingBar.setRating(Integer.parseInt(commodity.getStarNo()));
		mRatingBar.setClickable(false);
		
		mTitleBar = (GTitleBar) findViewById(R.id.mTitleBar);
		 mTitleBar.initLeftButton("", R.drawable.titlebar_back, new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				GoBack.goBack(ProductDetailActivity.this);
			}
		});
        mTitleBar.initTitle("商品详情");
        
        tvCheckTimes = (TextView) findViewById(R.id.tvCheckTimes);
        tvCheckTimes.setText(commodity.getViewNo());
        
        ivProductImg = (ImageView) findViewById(R.id.ivProductImg);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_BACK){
			GoBack.goBack(this);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void setGoBack() {
		Bundle bundle = new Bundle();
		bundle.putString("back2", getClass().getName());
		GoBack.goBackStack.add(bundle);
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void httpCompleted(ResponseInterface resp) {
		// TODO Auto-generated method stub
		SpecialResponse response = (SpecialResponse)resp;
		Logger.d("content:" + response.content);
//		s = JsonParser.getLoginInfo(response.content);
//		if(!s.getStatus().equals("200")){
//			Message msg = handler.obtainMessage();
//			msg.obj = s.getMessage();
//			msg.what = 0;
//			handler.sendMessage(msg);
//		}else{
//			MyApp.weiboUser.userid = s.getUserid();
//			Logger.d("--->" + s.getUserid());
//			handler.sendEmptyMessage(1);
//		}
	}
	
	
	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			closeProgress();
			switch(msg.what){
			case 0:
				showToast((String)msg.obj);
				break;
			case 1:
				break;
			}
		};
	};
	
	

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void getIntentData() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void setIntentData() {
		// TODO Auto-generated method stub
		
	}
	
	public void location(View view){
		Intent intent = new Intent(this,MyMapActivity.class);
		startActivity(intent);
	}
	
	public void mRatingBar(View view){
		
	}

	public void share(View view){
	
	}
	
	public void report(View view){
		
	}
	
	public void ivLarge(View view){
		
	}
	
	
	class LoadImageThread extends Thread {
		LoadImageThread() {
		}

		public void run() {
			File cacheFile = CacheFetch.dowanLoadBitmap(ProductDetailActivity.this,
					commodity.getImagePath_middle());
			Logger.d("middle:" +cacheFile.getPath());
			try {
				bitmap = BitmapFactory.decodeFile(cacheFile.getPath());
				drawable = Drawable.createFromPath(cacheFile.getPath());
			} catch (Exception e) {
				e.printStackTrace();
			}
			// if (bitmap != null) {

			// 此处不能直接更新UI，否则会发生异常：
			// CalledFromWrongThreadException: Only the original thread that
			// created a view hierarchy can touch its views.
			// 也可以使用Handler或者Looper发送Message解决这个问题

			ProductDetailActivity.this.runOnUiThread(new Runnable() {
				public void run() {
					if (bitmap != null) {// 此处在线程过多时可能为null
//						int width = bitmap.getWidth();// 获取真实宽高
//						int height = bitmap.getHeight();
//
//						LayoutParams lp = getLayoutParams();
//
//						int layoutHeight = (height * getItemWidth()) / width;// 调整高度
//						if (lp == null) {
//							lp = new LayoutParams(getItemWidth(), layoutHeight);
//						}
//						setLayoutParams(lp);

						setImageBitmap(bitmap);
//						setImageDrawable(drawable);
//						Handler h = getViewHandler();
//						Message m = h.obtainMessage(Constants.HANDLER_WHAT,
//								width, layoutHeight, FlowView.this);
//						h.sendMessage(m);
					}
				}

				private void setImageBitmap(Bitmap bitmap) {
					// TODO Auto-generated method stub
					ivProductImg.setImageBitmap(bitmap);
				}
				
				private void setImageDrawable(Drawable drawable) {
					// TODO Auto-generated method stub
					ivProductImg.setBackgroundDrawable(drawable);
				}
				
			});
		}
	}
	
	
}
